package com.sharefang.aproject.activity;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.andexert.library.RippleView;
import com.sharefang.aproject.R;
import com.sharefang.aproject.bean.SelfBean;
import com.sharefang.aproject.presenter.UserResultPresenter;
import com.sharefang.aproject.utils.CheckUtil;
import com.sharefang.aproject.utils.UiUtil;
import com.sharefang.aproject.view.IUserLoginView;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2016/8/10.
 */

public class LoginActivity extends Activity implements IUserLoginView, RippleView.OnRippleCompleteListener {
    Context tag = LoginActivity.this;
    CircleImageView civ_header;
    EditText et_user_name;
    EditText et_password;
    Button btn_login;
    Button btn_register;
    Button btn_forget_password;
    ImageButton ib_wechat_login;
    UserResultPresenter presenter = new UserResultPresenter(this, this);
    private int lastKeyboardHeight;
    private int offset = 0;
    //为解决软键盘弹出遮挡输入的问题，通过移动布局来适配UI显示
    private RelativeLayout edit_container;
    private int btn_registerY = 0;
    RippleView rv_login, rv_register, rv_forget_password, rv_wechat_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置为全屏模式
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_login);

        findView();
        initView();
        //键盘适配
        adapteKeyboard();
    }

    private void initView() {
        et_user_name.addTextChangedListener(textWatcher);
        et_password.addTextChangedListener(textWatcher);

//        btn_login.setOnClickListener(this);
//        btn_forget_password.setOnClickListener(this);
//        btn_register.setOnClickListener(this);
//
//        ib_wechat_login.setOnClickListener(this);

        rv_login.setOnRippleCompleteListener(this);
        rv_wechat_login.setOnRippleCompleteListener(this);
        rv_register.setOnRippleCompleteListener(this);
        rv_forget_password.setOnRippleCompleteListener(this);
    }

    private void findView() {
        civ_header = (CircleImageView) findViewById(R.id.civ_header);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        et_password = (EditText) findViewById(R.id.et_password);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_forget_password = (Button) findViewById(R.id.btn_forget_password);
        ib_wechat_login = (ImageButton) findViewById(R.id.ib_wechat_login);
        edit_container = (RelativeLayout) findViewById(R.id.edit_container);

        rv_login = (RippleView) findViewById(R.id.rv_login);
        rv_forget_password = (RippleView) findViewById(R.id.rv_forget_password);
        rv_register = (RippleView) findViewById(R.id.rv_register);
        rv_wechat_login = (RippleView) findViewById(R.id.rv_wechat_login);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tag = null;

    }

    @Override
    public String getUserName() {
        return et_user_name.getText().toString();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString();
    }

    @Override
    public void showLoading() {
        UiUtil.showLoadingDialog(LoginActivity.this, "正在登录");
    }

    @Override
    public void hideLoading() {
        UiUtil.hideLoadingDialog();
    }

    @Override
    public void toMainActivity() {

        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        LoginActivity.this.finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public void showFailedError(String message) {
        UiUtil.snake(LoginActivity.this, message);
    }

    private void doLogin() {
        if (UiUtil.isFastClick()) {
            return;
        }
        boolean canLogin = getUserName().length() != 0 && getPassword().length() != 0;
        if (canLogin) {
            if (CheckUtil.isMobileNO(getUserName()))
                presenter.login();
            else UiUtil.snake(LoginActivity.this, "手机号格式不正确");
        } else {
            UiUtil.snake(LoginActivity.this, "用户名或密码不能为空");
            return;
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            toggleLoginButton();
        }
    };

    /**
     * 切换登录按钮状态
     */
    private void toggleLoginButton() {
        boolean canLogin = et_user_name.getText().length() != 0 && et_password.getText().length() != 0;
        if (canLogin) {
            btn_login.setTextColor(tag.getResources().getColor(R.color.text_color_white));
            btn_login.setBackgroundColor(tag.getResources().getColor(R.color.main_color_green));
        } else {
            btn_login.setBackgroundColor(tag.getResources().getColor(R.color.text_color_white));
            btn_login.setTextColor(tag.getResources().getColor(R.color.main_color_green));
        }
    }

    /**
     * 适配软键盘遮挡
     */
    private void adapteKeyboard() {
        UiUtil.observeSoftKeyboard(LoginActivity.this, new UiUtil.OnSoftKeyboardChangeListener() {
            @Override
            public void onSoftKeyBoardChange(int softKeybardHeight, boolean visible) {
                initOffset();
                if (visible) {
                    civ_header.setVisibility(View.GONE);
                    UiUtil.slideView(edit_container, "translationY", 0, offset - softKeybardHeight);
                    ObjectAnimator.ofFloat(edit_container, "translationY", 0, offset - softKeybardHeight).setDuration(500).start();//setInterpolator(new OvershootInterpolator());
                    lastKeyboardHeight = offset - softKeybardHeight;

                } else {
                    civ_header.setVisibility(View.VISIBLE);
                    if (lastKeyboardHeight != 0) {
                        UiUtil.slideView(edit_container, "translationY", lastKeyboardHeight, 0);
                    }
                    lastKeyboardHeight = 0;
                }
            }
        });
    }

    /**
     * 计算位移高度
     */
    private void initOffset() {
        int[] register_location = new int[2];
        btn_register.getLocationOnScreen(register_location);
        if (btn_registerY == 0) {
            btn_registerY = register_location[1];
        }

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        final int height = wm.getDefaultDisplay().getHeight();
        offset = height - btn_registerY;
    }

    @Override
    public void onComplete(RippleView rippleView) {
        switch (rippleView.getId()) {
            case R.id.rv_login:
                doLogin();
                break;
            case R.id.rv_forget_password:
                break;
            case R.id.rv_register:
                break;
            case R.id.rv_wechat_login:
                break;
        }
    }
}
